import { defineStore } from 'pinia'
import { api, API_PREFIX, API_TSL_FRONTEND } from 'boot/axios'
import { Loading } from 'quasar'

// Should be use for orderRoutingHistory
export const useWoRoutingStore = defineStore('woroutinghist', {
  state: () => ({

    routeTable: [], // For WOFlowPage (structured)
    routingTable: [], // Raw from api
    routeTableOptions: null,
    opt_difficulty: [],
    opt_flow: [],
    opt_stone_mount_list: [],
    order_route: null,
    order_no: null

  }),
  getters: {
    getOrderRoutingTable: (state) => {
      return state.routeTable
    }
  },
  actions: {

    setParams(routingTable, routeTableOptions, OrderNo){
      // Loading.show()
      this.order_no = OrderNo
      this.routeTable = []
      this.routingTable = routingTable
      console.log('------------------RoutingTable: ', this.routingTable)
      
      this.routeTableOptions = routeTableOptions
      console.log('------------------routeTableOptions: ', this.routeTableOptions)
      
      // Difficulty
      for (const diff of this.routeTableOptions.difficulty){
        let temp_diff = {
          label: diff.name,
          value: diff.pk
        }
        this.opt_difficulty.push(temp_diff)
      }
      console.log('------------------opt_difficulty: ', this.opt_difficulty)
      
      // Flow
      for (const f of this.routeTableOptions.flow){
        let temp_flow = {
          label: f.chinese_name,
          value: f.pk
        }
        this.opt_flow.push(temp_flow)
      }
      console.log('------------------opt_flow: ', this.opt_flow)
      
      // Stone Mount List
      for (const sml of this.routeTableOptions.stone_mount_list){
        let temp_sml = {
          label: sml.name,
          value: sml.pk
        }
        this.opt_stone_mount_list.push(temp_sml)
      }
      console.log('------------------opt_stone_mount_list: ', this.opt_stone_mount_list)

       // Generate Route Table For WOFlowPage
       for (let i = 0; i < this.routingTable.length; i++) {
        let flow_key = this.opt_flow.find(optf => optf.label === this.routingTable[i].flow) ?? null
        let diff_key = this.opt_difficulty.find(optd => optd.label === this.routingTable[i].difficulty) ?? null
        let ps_key = this.opt_stone_mount_list.find(osm_ps => osm_ps.value === this.routingTable[i].primary_stone_setting) ?? null
        let ss_key = this.opt_stone_mount_list.find(osm_ss => osm_ss.value === this.routingTable[i].secondary_stone_setting) ?? null
        const rtFlow = {
          stepNo: '步骤 ' + this.routingTable[i].seq,
          // routeName: this.routingTable[i].flow,
          routeName: flow_key,
          qty: this.routingTable[i].qty,
          difficulty: diff_key,
          psName: ps_key,
          ssName: ss_key,
          consumePercent: this.routingTable[i].consumption_percentage,
          workfee: this.routingTable[i].worker_fee,
          // other stuff
          status: (this.routingTable[i].status === 'Open') ? 1 : (this.routingTable[i].status === 'In Progress') ? 2 : 3,
          pk: this.routingTable[i].pk,
          order_route: this.routingTable[i].order_route 
        }
        
        this.routeTable.push(rtFlow)
        this.order_route = this.routingTable[i].order_route
      }

      console.log('routeTable: ', this.routeTable)
      // Loading.hide()
    },

    async saveRoutingTable (rtRows, orderNo){
      console.log('in saveroutingTable')
      Loading.show()
      const params = []
      const wholeRT = []
      let i = 0
      for (const rt of rtRows) {
        i += 1
        let temp = {
          seq: i,
          pk: rt.pk,
          status: rt.status,
          flow: (rt.routeName) ? rt.routeName.value : null,
          quantity: rt.qty,
          consumption_percentage: rt.consumePercent,
          worker_fee: rt.workfee,
          difficulty: (rt.difficulty) ? rt.difficulty.value : null,
          primary_stone_setting: (rt.psName) ? rt.psName.value : null,
          secondary_stone_setting: (rt.ssName) ? rt.ssName.value : null,
          order_route: rt.order_route
        }
        if(temp.status === 1){
          params.push(temp)
        }
        wholeRT.push(temp)
      }
      console.log('params: ', params)
      console.log('whole routing table: ', wholeRT)
      try {
        const res = await api.post(API_TSL_FRONTEND + '/OrderRoute/' + 'update_order_routing/', { routing: params, order_no: orderNo})
        console.log('saveRoutingTable res: ', res)
        if(res.data.message === "Success"){
          console.log('update production remark success')
          this.routeTable = rtRows
        }else{
          console.log('update production remark failed')
        }
        Loading.hide()
        return true
      } catch (error) {
        console.log(error)
        Loading.hide()
        return false
      }
    }    
  }
})
